#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>

using namespace std;
using PII = pair<int, int>;
const int N = 410;

int n, m;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
int state[N];

void add(int a, int b, int c){
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx++;
}

int dijkstra(){
    memset(dist, 0x3f, sizeof dist);
    priority_queue<PII, vector<PII>, greater<PII>> que;
    que.push({0, 1});
    dist[1] = 0;

    while(que.size()){
        PII temp = que.top();
        que.pop();
        int ver = temp.second, dis = temp.first;
        if(state[ver]) continue;

        state[ver] = true;

        for (int u = h[ver]; u != -1; u = ne[u]){
            int j = e[u];

            if(dist[j] > dis + w[u]){
                dist[j] = dis + w[u];
                que.push({dist[j], j});
            }
        }

  
    }

    int minv = 0;
    for (int i = 1; i <= n; i ++){
        minv = max(minv, dist[i]);
    }

    if(minv ==  0x3f3f3f3f){
        return -1;
    }else{
        return minv;
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    cin >> n >> m;

    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i ++){
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }

    int k = dijkstra();

    cout << k;

    return 0;
}
